// 定义全局变量来存储当前语言和翻译
globalThis.currentLanguage = 'zh';
globalThis.translations = {
    en: {
        'cta.alert': 'Welcome to my portfolio! Please browse my projects.',
        'form.alert.success': 'Thank you for your message! I will reply to you as soon as possible.',
        'form.alert.error': 'Please fill in all fields.'
    },
    zh: {
        'cta.alert': '欢迎访问我的作品集！请浏览我的项目。',
        'form.alert.success': '感谢您的消息！我会尽快回复您。',
        'form.alert.error': '请填写所有字段。'
    }
};

// 翻译函数
globalThis.translate = function(key) {
    return globalThis.translations[globalThis.currentLanguage][key] || key;
};

// DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // CTA按钮点击事件
    const ctaButton = document.getElementById('cta-button');
    if (ctaButton) {
        ctaButton.addEventListener('click', function() {
            alert(translate('cta.alert'));
        });
    }

    // 表单提交事件
    const contactForm = document.getElementById('contact-form');
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单元素
            const nameInput = document.getElementById('name');
            const emailInput = document.getElementById('email');
            const messageInput = document.getElementById('message');
            
            // 重置之前的错误状态
            nameInput.style.borderColor = '';
            emailInput.style.borderColor = '';
            messageInput.style.borderColor = '';
            
            // 获取表单数据
            const name = nameInput.value.trim();
            const email = emailInput.value.trim();
            const message = messageInput.value.trim();
            
            // 验证邮箱格式
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            let isValid = true;
            
            // 简单验证
            if (!name) {
                nameInput.style.borderColor = '#f44336';
                isValid = false;
            }
            
            if (!email || !emailRegex.test(email)) {
                emailInput.style.borderColor = '#f44336';
                isValid = false;
            }
            
            if (!message) {
                messageInput.style.borderColor = '#f44336';
                isValid = false;
            }
            
            if (isValid) {
                // 模拟发送数据
                console.log('表单数据:', { name, email, message });
                
                // 显示成功消息
                alert(translate('form.alert.success'));
                contactForm.reset();
                
                // 添加提交成功的视觉反馈
                const submitButton = contactForm.querySelector('button[type="submit"]');
                const originalText = submitButton.textContent;
                
                submitButton.textContent = '已发送！';
                submitButton.style.backgroundColor = '#4caf50';
                
                // 3秒后恢复按钮状态
                setTimeout(function() {
                    submitButton.textContent = originalText;
                    submitButton.style.backgroundColor = '';
                }, 3000);
            } else {
                alert(translate('form.alert.error'));
            }
        });
    }

    // 平滑滚动到锚点
    const navLinks = document.querySelectorAll('.nav-links a');
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            const targetId = this.getAttribute('href');
            if (targetId.startsWith('#')) {
                e.preventDefault();
                const targetSection = document.querySelector(targetId);
                if (targetSection) {
                    window.scrollTo({
                        top: targetSection.offsetTop - 80, // 考虑固定导航栏的高度
                        behavior: 'smooth'
                    });
                }
            }
        });
    });

    // 简单的交互效果
    const projectCards = document.querySelectorAll('.project-card');
    projectCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = '0 5px 15px rgba(0,0,0,0.2)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
            this.style.boxShadow = '0 2px 5px rgba(0,0,0,0.1)';
        });
    });
});

// 窗口滚动事件
window.addEventListener('scroll', function() {
    const header = document.querySelector('header');
    if (window.scrollY > 50) {
        header.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)';
    } else {
        header.style.boxShadow = '0 2px 5px rgba(0,0,0,0.1)';
    }
});